#include "DeviceTool.h"
#include <QDebug>

DeviceTool::DeviceTool(QObject *parent)
    : QObject{parent}
{}

DeviceInfo DeviceTool::gainDeviceInfo(AVFormatContext *formatContext) {
    DeviceInfo deviceInfo = DeviceInfo();
    if (avformat_find_stream_info(formatContext, nullptr) < 0) {
        return deviceInfo;
    }
    for (unsigned int i = 0; i < formatContext->nb_streams; ++i) {
        AVStream *stream = formatContext->streams[i];
        AVCodecParameters *codecParams = stream->codecpar;
        if (codecParams->codec_type == AVMEDIA_TYPE_AUDIO) {
            deviceInfo.mediaType = AVMEDIA_TYPE_AUDIO;
            deviceInfo.sampleRate = codecParams->sample_rate;
            deviceInfo.numChannels = codecParams->ch_layout.nb_channels;
            deviceInfo.bitsPerSample = codecParams->bits_per_coded_sample;
            deviceInfo.codecId = codecParams->codec_id;
            deviceInfo.format = codecParams->format;
            deviceInfo.bit_rate = codecParams->bit_rate >> 3;
            deviceInfo.ch_layout = codecParams->ch_layout;
        } else if (codecParams->codec_type == AVMEDIA_TYPE_VIDEO) {
            deviceInfo.mediaType = AVMEDIA_TYPE_VIDEO;
            deviceInfo.codecId = codecParams->codec_id;
            deviceInfo.format = codecParams->format;
            deviceInfo.width = codecParams->width;
            deviceInfo.height = codecParams->height;
        }
    }
    return deviceInfo;
}
